package com.renjibo.sharkerwave.ui.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kevin.wraprecyclerview.WrapAdapter;
import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.adapter.HomeAdapter;
import com.renjibo.sharkerwave.models.CourseDetail;
import com.renjibo.sharkerwave.models.data.CourseData;
import com.renjibo.sharkerwave.network.SharkerParams;
import com.renjibo.sharkerwave.ui.fragment.FreeAuditionFragment;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 免费试听和精选课程页面
 */
public class FreeAuditionActivity extends BaseActivity implements View.OnClickListener {
    private List<Fragment> fragmentList = new ArrayList<>();
    private String[] arr = {"精选", "美容", "美甲", "按摩", "培训"};
    private TextView free_textView_title;
    private LinearLayout free_linearLayout;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_free_audition);

        Toolbar free_toolbar = (Toolbar) findViewById(R.id.free_toolbar);
        setSupportActionBar(free_toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        setData();
        initView();
        if(name !=null) {
            if(name.equals("精选课程")){
                //精选课程
                free_linearLayout.setVisibility(View.VISIBLE);
                //listCourse("list_course","",20,0,1);
            }else{
                //请求网络  免费试听
               // listTry("list_try","",20,0);
            }
            free_textView_title.setText(name + "");
        }
    }

    private void setData() {
        for (int i = 0; i < arr.length; i++) {
            fragmentList.add(FreeAuditionFragment.newInstance(name, arr[i]));
        }
    }

    private void initView() {
        ViewPager free_viewPager = (ViewPager) findViewById(R.id.free_viewPager);
        TabLayout free_tableLayout = (TabLayout) findViewById(R.id.free_tableLayout);
        free_linearLayout = (LinearLayout) findViewById(R.id.free_linearLayout);
        ImageView free_imageView_left = (ImageView) findViewById(R.id.free_imageView_left);
        free_textView_title = (TextView) findViewById(R.id.free_textView_title);

        free_imageView_left.setOnClickListener(this);
        free_viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return arr.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return arr[position];
            }
        });
        free_tableLayout.setupWithViewPager(free_viewPager);
        free_tableLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //图片关闭页面
            case R.id.free_imageView_left:
                finish();
                break;
        }
    }
    /**
     * 试听课程列表
     *
     * @param uri
     * @param category
     * @param page_index
     */
    void listTry(String uri, String category, int page_size, int page_index) {
        listCourse(uri, category, page_size, page_index, 0);
    }

    /**
     * 精品课程列表
     *
     * @param uri
     * @param category
     * @param page_index
     * @param sort_by
     */
    public void listCourse(final String uri, final String category, int page_size, int page_index, int sort_by) {
        SharkerParams params = new SharkerParams(uri);

        if (!TextUtils.isEmpty(category)) {
            params.addBodyParameter("category", category);
        }

        params.addBodyParameter("page_size", page_size + "");

        params.addBodyParameter("page_index", "" + page_index);

        if (sort_by != 0) {
            params.addBodyParameter("sort_by", "" + sort_by);
        }

        x.http().post(params, new Callback.CommonCallback<CourseData>() {
            @Override
            public void onSuccess(CourseData result) {

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
